#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList)
    {
        unordered_set<string> vis(wordList.begin(), wordList.end());
        unordered_set<string> hash;

        if (beginWord == endWord) return 0;
        if (!vis.count(endWord)) return 0;

        queue<string> q;
        q.push(beginWord);
        hash.insert(beginWord);

        int n = beginWord.size();

        int ret = 1;
        while (q.size())
        {
            int sz = q.size();
            ret++;
            while (sz--)
            {
                string t = q.front();
                q.pop();
                for (int i = 0; i < n; i++)
                {
                    string tmp = t;
                    for (char ch = 'a'; ch <= 'z'; ch++)
                    {
                        tmp[i] = ch;
                        if (vis.count(tmp) && !hash.count(tmp))
                        {
                            if (tmp == endWord) return ret;
                            q.push(tmp);
                            hash.insert(tmp);
                        }

                    }
                }
            }
        }
        return 0;
    }
};